package bbs.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.orm.SugarRecord;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.UIHandler;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import bbs.adapter.CommitBackAdapter;
import bbs.model.CommitBack;
import bbs.model.Submit;
import bbs.model.User;
import bbs.net.GetCommitBack;
import bbs.net.SendCommitBack;
import model.OwnUser;
import utils.TimeUtil;

/**
 * 对某人的帖子进行回复
 * Created by nimi on 2015/11/12.
 */
public class OnePostActivity extends UIHandler.BaseActivity implements GetCommitBack.Delegate, SendCommitBack.Delegate {
    private Button backBBSMAIN;
    private TextView bbs_title;//标题条
    private TextView postName;
    private TextView postTime;
    private TextView username;
    private TextView postContent;
    private RelativeLayout thepost;
    private LinearLayout tiezi_reply;
    long submitID;
    private GetCommitBack getCommitBack;
    private CommitBackAdapter commitBackAdapter;
    private ListView listcommitBack;
    private List<GetCommitBack.BoardFeedback> commitBacks = new ArrayList<GetCommitBack.BoardFeedback>();
    private EditText edit_reply;//回帖内容
    private ImageButton btn_reply;//回帖button
    private SendCommitBack.Upload upload = new SendCommitBack.Upload();
    CommitBack commitBack = new CommitBack();
    User user = new User();
    long hours;
    long days;
    private boolean scrollFlag = false;// 标记是否滑动

    OwnUser ownUser;
    {
        List<OwnUser> ownUsers = SugarRecord.listAll(OwnUser.class);
        if (!ownUsers.isEmpty()) {
            ownUser = ownUsers.get(0);
        }
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.bbs_show);
        backBBSMAIN = (Button) findViewById(R.id.back_bbs);
        backBBSMAIN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(OnePostActivity.this, BbsMainActivity.class);
//                startActivity(intent);
                finish();
            }
        });
        listcommitBack = (ListView) findViewById(R.id.post_content);
        View headView = getLayoutInflater().inflate(R.layout.bbs_show_main, null);
        listcommitBack.addHeaderView(headView);
        commitBackAdapter = new CommitBackAdapter(this, commitBacks);
        listcommitBack.setAdapter(commitBackAdapter);
        initPost();//帖子展现
        initReply();//回复展现


    }

    public void initPost() {
        Bundle bundle = getIntent().getExtras();
        submitID = Long.parseLong(bundle.getString("submitID"));
        String submitTitle = bundle.getString("submitName");
        String subjectName = bundle.getString("subjectName");
        String submitcontent = bundle.getString("submitContent");
        String submitTime = bundle.getString("submitTime");
        String userName = bundle.getString("userName");
        thepost = (RelativeLayout) findViewById(R.id.thepost);
        tiezi_reply = (LinearLayout) findViewById(R.id.tiezi_reply);
        bbs_title = (TextView) findViewById(R.id.bbs_title);
        bbs_title.setText(subjectName);//设置该贴的主题显示在上面
        postName = (TextView) findViewById(R.id.postName);
        postName.setText(submitTitle);
        postTime = (TextView) findViewById(R.id.postTime);
        String timeNow = TimeUtil.currentTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = df.parse(submitTime);
            Date d2 = df.parse(timeNow);
            long diff = d2.getTime() - d1.getTime();
            days = diff / (1000 * 60 * 60 * 24);
            hours = diff / (1000 * 60 * 60);

        } catch (Exception e) {

        }
        if (days >= 1)
            postTime.setText((int) days + "天前");
        else {
            postTime.setText((int) hours + "小时前");
        }
        username = (TextView) findViewById(R.id.username);
        username.setText(userName);
        postContent = (TextView) findViewById(R.id.postContent);
        postContent.setText(submitcontent);
        edit_reply = (EditText) findViewById(R.id.edit_reply);
        btn_reply = (ImageButton) findViewById(R.id.btn_reply);
        btn_reply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (check()) {

                    commitBack.setFrom(user);
                    Submit submit = new Submit();
                    submit.setBoard_ID(submitID+"");
                    commitBack.setSubmit(submit);
                    commitBack.setContentBack(edit_reply.getText().toString());
                    commitBack.setTime(TimeUtil.currentTime().toString());
                    upload.setCommitBack(commitBack);
                    send();
                }
            }
        });

    }

    public void initReply() {
        getCommitBack = new GetCommitBack(submitID);
        getCommitBack.setDelegate(OnePostActivity.this);
        getCommitBack.get();
    }

    public boolean check() {
        if (TextUtils.isEmpty(edit_reply.getText().toString())) {
            Toast.makeText(OnePostActivity.this, "未填写任何内容", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    public void send() {
        SendCommitBack sendCommitBack = new SendCommitBack(upload);
        sendCommitBack.setDelegate(OnePostActivity.this);
        sendCommitBack.get();
    }

    public void addThenew() {
        GetCommitBack.BoardFeedback feedback = new GetCommitBack.BoardFeedback();
        feedback.setFeedback_Time(commitBack.getTime());
        feedback.setFeedback_Content(commitBack.getContentBack());
        feedback.setBoard_ID(commitBack.getSubmit().getBoard_ID());
        feedback.setUser_RealName(ownUser.getUser_RealName());
        feedback.setUser_ID(ownUser.getUser_ID());
        commitBacks.add(commitBacks.size(), feedback);
        commitBackAdapter.notifyDataSetChanged();
        commitBack = new CommitBack();

    }

    public void onResponse(String response, GetCommitBack getCommitBack) {
        if (getCommitBack.getDownload().getStatus() == 1) {
            commitBacks.addAll(getCommitBack.getDownload().getBbs_Feedback());
            commitBackAdapter.notifyDataSetChanged();
        }


    }

    public void onErrorResponse(VolleyError error, GetCommitBack getCommitBack) {
        Toast.makeText(this, "网络问题，请稍后再试", Toast.LENGTH_SHORT).show();

    }

    public void onResponse(String response, SendCommitBack sendCommitBack) {
        if (sendCommitBack.getDownload().getStatus() == 1) {
            edit_reply.setText("");
            Toast.makeText(this, "回复成功", Toast.LENGTH_SHORT).show();
            addThenew();
        }

    }

    public void onErrorResponse(VolleyError error, SendCommitBack sendCommitBack) {
        Toast.makeText(this, "网络问题，请稍后再试", Toast.LENGTH_SHORT).show();

    }

}
